package com.zhongwang.mms.module.buy.service;

import com.zhongwang.mms.gen.entity.*;
import com.zhongwang.mms.gen.mapper.BReportPlanMapper;
import com.zhongwang.mms.module.buy.dao.BuyPlanDao;
import com.zhongwang.mms.module.buy.dao.RawMatReturnDao;
import com.zhongwang.mms.module.buy.dao.ReportPlanDao;
import com.zhongwang.mms.module.buy.model.ShoppingPlanModel;
import com.zhongwang.mms.module.system.log.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Description: 原材料回货 Service
 *
 * @Author: China.sgl
 * @Date: 2020/4/7 16:25
 */
@SuppressWarnings("ALL")
@Service
public class MaterialReturnService {

    @Autowired
    private BuyPlanDao buyPlanDao;
    @Autowired
    private LogService logService;
    @Autowired
    private BReportPlanMapper bReportPlanMapper;
    @Autowired
    private RawMatReturnDao rawMatReturnDao;
    @Autowired
    private ReportPlanDao reportPlanDao;

    /**
     * 查询外购件回货列表
     *
     * @param shoppingPlanModel 条件
     * @return list
     */
    public List<ShoppingPlanModel> findMaterialReturn(ShoppingPlanModel shoppingPlanModel) {
        return rawMatReturnDao.findMaterialReturnList(shoppingPlanModel);
    }
    /**
     * 查询外购件回货列表
     *
     * @param shoppingPlanModel 条件
     * @return list
     */
    public Integer findMaterialReturnCount(ShoppingPlanModel shoppingPlanModel) {
        return rawMatReturnDao.findMaterialReturnCount(shoppingPlanModel);
    }

    /**
     * 确认回货
     *
     * @param rppId 采购计划ID
     * @param odaId 入库记录ID
     * @return 行数
     */
    @Transactional(rollbackFor = Exception.class)
    public int submitReturn(Integer rppId, Integer odaId) {
        BReportPlan plan = bReportPlanMapper.selectByPrimaryKey(rppId);
        MOrderDetail mOrderDetail = rawMatReturnDao.getMOrderDetailByPrimaryKey(odaId);
        MMaterials mMaterials = rawMatReturnDao.getMMaterialsByPrimaryKey(mOrderDetail.getOdaMatId());
        //更新提报计划的确认回货金额和确认回货时间
        plan.setRppBuyPrice(mOrderDetail.getOdaPrice());
        plan.setRppConfirmReturnDate(new Date());
        plan.setRppSapCode(mOrderDetail.getOdaMatSapCode());
        plan.setRppBuyStockMatDesc(mMaterials.getMatNameDesc());
        logService.saveLog("原材料确认回货", "物料描述：" + plan.getRppMatDesc());
        plan.setRppOdaId(odaId);
        plan.setRppRetStatus(1);
        plan.setRppBuyStockMatDesc(mMaterials.getMatNameDesc());
        plan.setRppConfirmReturnDate(new Date());
        return bReportPlanMapper.updateByPrimaryKey(plan);
    }

    /**
     * 批量确认回货
     *
     * @param rppId 采购计划主键数组
     * @param odaId 入库记录主键数组
     * @return 行数
     */
    @Transactional(rollbackFor = Exception.class)
    public int submitReturn(Integer[] rppId, Integer[] odaId) {
        for (int i = 0; i < rppId.length; i++) {
            submitReturn(rppId[i], odaId[i]);
        }
        return 1;
    }

    /**
     * 撤消回货确认
     *
     * @param id 采购ID
     * @return 行数
     */
    public int cancelRetrun(Integer id) {
        BReportPlan plan = bReportPlanMapper.selectByPrimaryKey(id);
        logService.saveLog("撤消原材料回货确认", "物料描述：" + plan.getRppMatDesc());
        plan.setRppOdaId(null);
        plan.setRppRetStatus(0);
        plan.setRppBuyStockMatDesc(null);
        plan.setRppConfirmReturnDate(null);
        return bReportPlanMapper.updateByPrimaryKey(plan);
    }

    /**
     * 查询入库记录
     *
     * @param shoppingPlanModel 条件
     * @return list
     */
    public List<ShoppingPlanModel> findOrderDetail(ShoppingPlanModel shoppingPlanModel) {
        return buyPlanDao.findMaterialOrderDetail(shoppingPlanModel);
    }

}
